<?php

require_once '../../includes/include_admin.php';
require_once '../../classes/az_FileImage.php';

run_action();

/*
 * Funciton run action
 */

function run_action() {
    if (isset($_POST['act']) && $_POST['act'] != null) {
        $action = $_POST['act'];
    } else if (isset($_GET['act']) && $_GET['act'] != null) {
        $action = $_GET['act'];
    } else {
        $action = 0;
    }

    $html = null;
    switch ($action) {
        case 1:
            $html['html'] = add_new_image();
            break;
        case 2:
            delete_image();
            break;
        case 3:
            $html = get_list_image_tinymce_plugin();
            break;
        default:
            break;
    }

    /* Send as JSON */
    header("Content-Type: application/json", true);
    echo json_encode($html);
    exit();
}

/*
 * Funciton add new image
 */

function add_new_image() {
    if (isset($_FILES['image-0']) && $_FILES['image-0'] != null) {
        $filename = $_FILES['image-0']['name'];
        $filesize = $_FILES['image-0']['size'];
        if ($_FILES['image-0']['type'] === 'image/jpeg') {
            $filename_type = 'jpeg';
        } else if ($_FILES['image-0']['type'] === 'image/gif') {
            $filename_type = 'gif';
        } else if ($_FILES['image-0']['type'] === 'image/png') {
            $filename_type = 'png';
        }

        //Create image object
        $image = new az_FileImage();
        $image->create_image(null, $_FILES['image-0'], $filename, null, 1, $filename_type, $filesize, null);
        if ($image->add_image()) {
            return get_list_image();
        } else {
            return '<center><i>Không có hình ảnh nào trong hệ thống</i></center>';
        }
    } else {
        return '<center><i>Không có hình ảnh nào trong hệ thống</i></center>';
    }
}

/*
 * Function delete image
 */

function delete_image() {
    if (isset($_POST['image']) && is_array($_POST['image'])) {
        $image = new az_FileImage();
        $count = count($_POST['image']);
        $tmpcount = 0;
        foreach ($_POST['image'] as $imageId) {
            $rows = $image->get_one_image($imageId);
            if ($rows != null && is_array($rows)) {
                $img_name = $rows['name'];
                //set dir
                $imgDir = '../../../upload/image/';
                if ($rows['name_file_type'] === 'jpeg') {
                    $imgDir = $imgDir . 'jpg/root/' . $img_name;
                } else if ($rows['name_file_type'] === 'gif') {
                    $imgDir = $imgDir . 'gif/root/' . $img_name;
                } else if ($rows['name_file_type'] === 'png') {
                    $imgDir = $imgDir . 'png/root/' . $img_name;
                }
                //Check if file image avaliable
                if (is_file($imgDir)) {
                    if (unlink($imgDir)) {
                        $result = $image->delete_image($imageId);
                        if ($result) {
                            $tmpcount +=1;
                        }
                    }
                }
            }
        }
        if ($count == $tmpcount) {
            header('Location: index.php');
        }
    }
}

/*
 * Delete data of image and without delete file
 */

function delete_image_data() {
    if (isset($_POST['image']) && is_array($_POST['image'])) {
        $image = new az_FileImage();
        $count = count($_POST['image']);
        $tmpcount = 0;
        foreach ($_POST['image'] as $imageId) {
            $result = $image->delete_image($imageId);
            if ($result) {
                $tmpcount +=1;
            }
        }
        if ($count == $tmpcount) {
            header('Location: index.php');
        }
    }
}

/*
 * Function get list image
 */

function get_list_image() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'image/list_image';
    $listimg = $tpl->get_main($file_tpl, 2);

    //Create a $sql query for get all total category 
    $sql = "SELECT COUNT(*) FROM `az_file` WHERE `type_file`=1";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    $rootDir = '../../../upload/image/';
    //Create params for pager
    $scroll_page = 5;
    $per_page = 18;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM `az_file` WHERE `type_file`=1 ORDER BY `file_id` DESC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $imgId = $rows['file_id'];
            $imgname = $rows['name'];
            $imgInfo = $rows['info'];
            $name_file_type = $rows['name_file_type'];
            if ($name_file_type === 'jpeg') {
                $dirimg = $rootDir . 'jpg/root/';
            } else if ($name_file_type === 'gif') {
                $dirimg = $rootDir . 'gif/root/';
            } else if ($name_file_type === 'png') {
                $dirimg = $rootDir . 'png/root/';
            }
            $img_data .= '<div class="frm_img_div_img_item"><ul class="frm_list_img_ul">' .
                    '<li><a href="' . $dirimg . $imgname . '" rel="lightbox"><img src="' . $dirimg . $imgname . '" alt="Thư pháp" width="70" height="70" /></a></li>' .
                    '<li class="frm_img_item_info"><input type="checkbox" name="image[]" value="' . $imgId . '"/>' . $imgname . '&nbsp;' . $imgInfo . '</li></ul></div>';
        }
        $result_P->free();
        //Assign $html
        $html = $tpl->get_vars($listimg, array('list_img_block' => $img_data, 'page_par_left' => $pager->firt_page,
                    'page_left' => $pager->previous_page, 'page-info' => $pager->page_info,
                    'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
        return $html;
    } else {
        $result_P->free();
        $html = "<center><i>Không có hình ảnh nào trong hệ thống</i></center>";
        return $html;
    }
}

/*
 * Function get list image
 */

function get_list_image_tinymce_plugin() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();

    //Create a $sql query for get all total category 
    $sql = "SELECT COUNT(*) FROM `az_file` WHERE `type_file`=1";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    $rootDir = '../../../../../upload/image/';
    $radioImgDir = '../../../upload/image/';
    //Create params for pager
    $scroll_page = 5;
    $per_page = 9;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set_azplugin($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM `az_file` WHERE `type_file`=1 ORDER BY `file_id` DESC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $imgId = $rows['file_id'];
            $imgname = $rows['name'];
            $imgInfo = $rows['info'];
            $name_file_type = $rows['name_file_type'];
            if ($name_file_type === 'jpeg') {
                $dirimg = $rootDir . 'jpg/root/';
                $radioImg = $radioImgDir . 'jpg/root/';
            } else if ($name_file_type === 'gif') {
                $dirimg = $rootDir . 'gif/root/';
                $radioImg = $radioImgDir . 'gif/root/';
            } else if ($name_file_type === 'png') {
                $dirimg = $rootDir . 'png/root/';
                $radioImg = $radioImgDir . 'png/root/';
            }
            $img_data .= '<div class="lib_img_item"><ul class="frm_list_img_ul">' .
                    '<li><img src="' . $dirimg . $imgname . '" alt="Thư pháp" width="70" height="70" /></li>' .
                    '<li class="frm_img_item_info"><input type="radio" name="image" value="' . $radioImg . $imgname . '"/>' . $imgname . '&nbsp;' . $imgInfo . '</li></ul></div>';
        }
        $result_P->free();


        $page = '<div id ="pagger_frm_list_img" class="pagger_list_img"><ul><li>' . $pager->firt_page . '</li><li>' . $pager->previous_page . '</li><li>' .
                $pager->page_info . '</li><li>' . $pager->next_page . '</li><li>' . $pager->last_page . '</li></ul></div>';
        $img_data = '<div class="az_block_img_only">' . $img_data . '</div>' . $page;
        //Assign $html
        $html['html'] = $img_data;
        return $html;
    } else {
        $result_P->free();
        $html['html'] = "<center><i>Không có hình ảnh nào trong hệ thống</i></center>";
        return $html;
    }
}

?>
